<?php

namespace app\admins\controller;

use think\Controller;
use think\Db;
use Util\SysDb;

class News extends Controller
{
    public function newsAdd()
    {
        $userId = input('param.userId');
        $newsName = input('param.newsName');
        $abstract = input('param.abstract');
        $content = input('param.content');
        $newsImg = input('param.newsImg');
        $newsId = input('param.newsId');
        $newsTime = input('param.newsTime');
        $newsTop = input('param.newsTop');
        $picture = input('param.newsImg');
        if ($newsId == 'no') {
            //新增
            $this->db = new SysDb();

            $count = $this->db->table('newslist')->count();
            $data = [];
            $data['newsId'] = $count + 1;
            $data['publisher'] = $userId;
            $data['newsName'] = $newsName; //传入session中的用户名
            $data['abstract'] = $abstract;
            $data['picture'] = '';
            $data['newsTop'] = $newsTop;
            $data['newsImg'] = $newsImg;
            $data['newsTime'] = $newsTime;
            $data['content'] = $content;
            $add = $this->db->table('newslist')->insert($data);
            if ($add) {
                exit(json_encode(['code' => 0, 'msg' => '添加成功']));
            } else {
                exit(json_encode(['code' => 1, 'msg' => '添加失败']));
            }
        } else {//编辑
            $this->db = new SysDb();

            $count = $this->db->table('newslist')->count();
            $data = [];
            $data['newsId'] = $newsId;
            $data['publisher'] = $userId;
            $data['newsName'] = $newsName; //传入session中的用户名
            $data['abstract'] = $abstract;

            $data['newsTop'] = $newsTop;
            $data['newsImg'] = $newsImg;
            $data['newsTime'] = $newsTime;
            $data['content'] = $content;
            $data['picture'] = '';
            $update = $this->db->table('newslist')->where(['newsId' => $newsId])->update($data);
            if ($update) {
                exit(json_encode(['code' => 0, 'msg' => '修改成功']));
            } else {
                exit(json_encode(['code' => 1, 'msg' => '修改失败']));
            }
        }
    }

    public function newsList()
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //数据
            $data1 = $this->db->table('newslist')->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('newslist')->count();
            $tol = ($page - 1) * $limit;

            $list = db('newslist')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        } else {
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('newslist')->where('newsname', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('newslist')->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('newslist')->where('newsname', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        }

        $index_json = json_encode($index);

        echo $index_json;
    }

    public function newsDel()
    {
        $newsId = input('param.newsId');

        $this->db = new SysDb();
        $del = $this->db->table('newslist')->where(['newsId' => $newsId])->delete();
        if ($del) {
            $resetId = $this->db->table('newslist')->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                $this->db->table('newslist')->where(['newsId' => $rid['newsId']])->update(['newsId' => $re]);
                ++$re;
            }

            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function newsAllDel()
    {
        $allId = input('param.newsId');

        $this->db = new SysDb();
        foreach ($allId as $id) {
            $del = $this->db->table('newslist')->where(['newsId' => $id])->delete();
        }
        if ($del) {
            $resetId = $this->db->table('newslist')->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                $this->db->table('newslist')->where(['newsId' => $rid['newsId']])->update(['newsId' => $re]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }
}
